from datetime import datetime, date, time
from typing import List

from sqlalchemy.orm import Session

from module_admin.dao.cms_driver_dao import CmsDriverDao
from module_admin.dao.cms_order_dao import CmsOrderDao
from module_admin.dao.cms_order_dispatch_dao import CmsOrderDispatchDao
from module_admin.entity.do.cms_driver import CmsDriver
from module_admin.entity.do.cms_order import CmsOrder
from module_admin.entity.vo.cms_driver_vo import DriverModel, CrudDriverResponse, DriverForQuery, DriverOrderList, \
    OrderListOrderChild, OrderListForQueryAfter, OrderListForQuery
from utils.stringUtils import stringUtils
from utils.time_format_util import list_format_datetime
from utils.uuid import UUID


class CmsDriverService:

    @classmethod
    def create_driver(cls, result_db: Session, page_object: DriverModel):
        # 待审核
        uuid = UUID().uuid()
        page_object.id = int(uuid)
        page_object.driver_id = "dri_" + uuid
        page_object.state = 1
        # 如果没有账号默认手机号为账号
        if page_object.account is None or page_object.account.strip() is None:
            page_object.account = page_object.phone
        # 如果没有密码默认身份证后六位
        if page_object.password is None or page_object.password.strip() is None:
            page_object.password = stringUtils.get_last_num_chars(page_object.identity_card, 6)
        driver_model = stringUtils.get_one(page_object.driver_model_list)
        try:
            CmsDriverDao.create_driver(result_db, page_object, driver_model)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))

        return CrudDriverResponse(**result)

    @classmethod
    def delete_driver(cls, query_db: Session, driver_id: int):
        CmsDriverDao.delete_driver_by_id(query_db, driver_id)
        query_db.commit()

    @classmethod
    def update_driver(cls, db, driver_update: DriverModel):
        driver_update_dict = driver_update.dict(exclude_unset=True)
        driver_model = stringUtils.get_one(driver_update.driver_model_list)
        driver_update_dict.setdefault("driver_model", driver_model)
        if "driver_model_list" in driver_update_dict:
            del driver_update_dict["driver_model_list"]
        try:
            CmsDriverDao.update_driver_by_id(db, driver_update_dict)
            db.commit()
            result = dict(is_success=True, message='更新成功')
        except Exception as e:
            db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudDriverResponse(**result)

    @classmethod
    def get_driver(cls, query_db: Session, id: int):
        driver_by_id: CmsDriver = CmsDriverDao.get_one_by_id(query_db, id)
        driver_dict = cls.del_and_add_driver_model(driver_by_id)
        return DriverModel(**driver_dict)

    @classmethod
    def del_and_add_driver_model(cls, driver_by_id: CmsDriver):
        driver_model_list = stringUtils.get_list(driver_by_id.driver_model)
        driver_dict = driver_by_id.__dict__
        if "driver_model" in driver_dict:
            del driver_dict["driver_model"]
        driver_dict.setdefault("driver_model_list", driver_model_list)
        return driver_dict

    @classmethod
    def list(cls, db: Session, query_object: DriverForQuery):
        driver_list_result = CmsDriverDao.list(db, query_object)
        list0 = []
        for e in driver_list_result:
            driver_dict = cls.del_and_add_driver_model(e)
            model = DriverModel(**driver_dict)
            list0.append(model)
        return list0

    @classmethod
    def list_all(cls, db, order_id_num: int):
        # 单号查开始结束时间
        order: CmsOrder = CmsOrderDao.get_order_by_id(db, order_id_num)
        driver_list_result = None
        if order is None:
            driver_list_result = CmsDriverDao.list_all(db, None, None)
        else:
            driver_list_result = CmsDriverDao.list_all(db, order.plan_begin_time, order.plan_end_time)
        return driver_list_result

    @classmethod
    def list_all_for_urgent(cls, db, order_plan_time):
        return CmsDriverDao.list_all(db, order_plan_time.plan_begin_time, order_plan_time.plan_end_time)

    @classmethod
    def list_free(cls, db, order_id: int):
        # 单号查开始结束时间
        order: CmsOrder = CmsOrderDao.get_order_by_id(db, order_id)
        if order is None:
            return None
        driver_list_result = CmsDriverDao.list_free(db, order.plan_begin_time, order.plan_end_time)
        list0 = []
        for e in driver_list_result:
            driver_dict = cls.del_and_add_driver_model(e)
            model = DriverModel(**driver_dict)
            list0.append(model)
        return list0

    @classmethod
    def list_free_for_urgent(cls, db, order_plan_time):
        driver_list_result = CmsDriverDao.list_free(db, order_plan_time.plan_begin_time, order_plan_time.plan_end_time)
        list0 = []
        for e in driver_list_result:
            driver_dict = cls.del_and_add_driver_model(e)
            model = DriverModel(**driver_dict)
            list0.append(model)
        return list0

    @classmethod
    def have_jxz(cls, db: Session, driver_id: str):
        result = CmsDriverDao.have_jxz(db, driver_id)
        return result

    @classmethod
    def order_list(cls, db, driver_order_query: OrderListForQuery):
        # 处理时间
        after = OrderListForQueryAfter()
        after.query_begin = driver_order_query.start_time
        after.query_end = driver_order_query.end_time
        list0 = []
        query = DriverForQuery()
        driver_list_result: List[CmsDriver] = CmsDriverDao.list(db, query)
        for e in driver_list_result:
            driver_order_list = DriverOrderList()
            driver_order_list.id = e.id
            driver_order_list.driver_id = e.driver_id
            driver_order_list.driver_name = e.driver_name
            after.driver_id = e.driver_id
            order_list_result = CmsOrderDispatchDao.list_by_driver_or_car(db, after)
            list_format_datetime(order_list_result)
            driver_order_list.order_list = order_list_result
            if order_list_result:
                list0.append(driver_order_list)
        return list0





